package com.memory.monitor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.memory.monitor.annotation.AuthCheck;
import com.memory.monitor.common.BaseResponse;
import com.memory.monitor.common.DeleteRequest;
import com.memory.monitor.common.ErrorCode;
import com.memory.monitor.common.ResultUtils;
import com.memory.monitor.constant.UserConstant;
import com.memory.monitor.exception.BusinessException;
import com.memory.monitor.exception.ThrowUtils;
import com.memory.monitor.model.dto.trend.TrendQuerryRequest;
import com.memory.monitor.model.entity.Trend;
import com.memory.monitor.model.vo.TrendVO;
import com.memory.monitor.service.TrendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/trend")
@Slf4j
public class TrendController {
    @Resource
    private TrendService trendService;

    /**
     * 分页获取用户列表（仅管理员）
     *
     * @param trendQuerryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<Trend>> listTrendByPage(@RequestBody TrendQuerryRequest trendQuerryRequest,
                                                    HttpServletRequest request) {
        long current = trendQuerryRequest.getCurrent();
        long size = trendQuerryRequest.getPageSize();


        Page<Trend> userPage = trendService.page(new Page<>(current, size));

        return ResultUtils.success(userPage);
    }

    /**
     * 创建用户
     *
     * @param trendVO
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addTrend(@RequestBody TrendVO trendVO, HttpServletRequest request) {
        if (trendVO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Trend trend = new Trend();
        BeanUtils.copyProperties(trendVO, trend);
        boolean result = trendService.save(trend);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(trend.getId());
    }

    /**
     * 删除用户
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteTrend(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = trendService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新用户
     *
     * @param trendVO
     * @param request
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateTrend(@RequestBody TrendVO trendVO,
                                            HttpServletRequest request) {
        if (trendVO == null || trendVO.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Trend trend = new Trend();
        BeanUtils.copyProperties(trendVO, trend);
        boolean result = trendService.updateById(trend);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }
}